package com.freecloud.framework.remotecall;

import com.freecloud.framework.config.FreeCloudConfig;
import com.freecloud.framework.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: maomao
 * @Date: 2019-08-15 09:17
 */
public abstract class RemoteCallFactory<T> {

    @Autowired
    private FreeCloudConfig freeCloudConfig;

    private List<T> callList;

    public RemoteCallFactory(List<T> list){
        callList = list;
    }



    public T call(){
        String type = freeCloudConfig.getRemoteCall();
        for(T call : callList){
            if(Constant.RemoteCallType.LOCAL.getValue().equals(type) && call instanceof LocalCall){
                return call;
            }else if(Constant.RemoteCallType.HTTP.getValue().equals(type)){
                if(!freeCloudConfig.isFeignEnabled()){
                    throw new UnsupportedOperationException(String.format("FeignEnabled未开启无法调用！%s call : %s ",type,callList));
                }

                if(call instanceof HttpCall || String.valueOf(call).contains("HardCodedTarget")){
                    return call;
                }
            }
        }
        throw new UnsupportedOperationException(String.format("没有可用的%s call : %s ",type,callList));
    }
}
